using System.Collections.Generic;
using System.Configuration;
using System.Security.Cryptography.X509Certificates;

namespace NHConfiguration
{
    public class ServiceElement : ConfigurationElement
    {
        [ConfigurationProperty("name", IsRequired = true)]
        public string Name
        {
            get { return (string)this["name"]; }
            set { this["name"] = value; }
        }

        [ConfigurationProperty("serverCertificate", IsRequired = false)]
        public string ServerCertificate
        {
            get { return (string)this["serverCertificate"]; }
            set { this["serverCertificate"] = value; }
        }

        [ConfigurationProperty("clientCertificates", IsRequired = false)]
        public string ClientCertificates
        {
            get { return (string)this["clientCertificates"]; }
            set { this["clientCertificates"] = value; }
        }

        public X509Certificate2 GetServerCertificate()
        {
            return CertificateHelper.LoadFromFile(ServerCertificate);
        }

        public IEnumerable<X509Certificate2> GetClientCertificates()
        {
            foreach (var file in ClientCertificates.Split(','))
            {
                var fullpath = file.Trim();

                if (string.IsNullOrEmpty(fullpath))
                    continue;
                fullpath = PathHelper.LocateServerPath(fullpath);
                var certificate =
                        new X509Certificate2(fullpath, string.Empty);
                yield return certificate;
            }
        }
    }
}